package com.ruoyi.web.controller.stock;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SeqNumberUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.stock.domain.WarehouseIn;
import com.ruoyi.stock.domain.vo.WarehouseInVo;
import com.ruoyi.stock.service.IWarehouseInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 入库Controller
 *
 * @author YwaiX
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/stock/in")
public class WarehouseInController extends BaseController {
    @Autowired
    private IWarehouseInService warehouseInService;

    /**
     * 查询入库列表
     */
    @PreAuthorize("@ss.hasPermi('stock:in:list')")
    @GetMapping("/list")
    public TableDataInfo list(WarehouseInVo warehouseIn) {
        startPage();
        List<WarehouseInVo> list = warehouseInService.selectWarehouseIns(warehouseIn);
        return getDataTable(list);
    }

    /**
     * 导出入库列表
     */
    @PreAuthorize("@ss.hasPermi('stock:in:export')")
    @Log(title = "入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarehouseIn warehouseIn) {
        List<WarehouseIn> list = warehouseInService.selectWarehouseInList(warehouseIn);
        ExcelUtil<WarehouseIn> util = new ExcelUtil<WarehouseIn>(WarehouseIn.class);
        util.exportExcel(response, list, "入库数据");
    }

    /**
     * 获取入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('stock:in:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(warehouseInService.selectWarehouseInById(id));
    }

    /**
     * 新增入库
     */
    @PreAuthorize("@ss.hasPermi('stock:in:add')")
    @Log(title = "入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarehouseIn warehouseIn) {
        return toAjax(warehouseInService.insertWarehouseIn(warehouseIn));
    }

    /**
     * 采购入库
     */
    @PreAuthorize("@ss.hasPermi('stock:in:add')")
    @Log(title = "入库", businessType = BusinessType.INSERT)
    @PostMapping("purchase")
    public AjaxResult addPurchase(@RequestBody WarehouseIn warehouseIn) {
        warehouseIn.setCreateBy(getUserId()+"");
        warehouseInService.insertWarehouseInPurchase(warehouseIn);
        return success(warehouseIn);
    }
    /**
     * 修改入库
     */
    @PreAuthorize("@ss.hasPermi('stock:in:edit')")
    @Log(title = "入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarehouseIn warehouseIn) {
        return toAjax(warehouseInService.updateWarehouseIn(warehouseIn));
    }

    /**
     * 删除入库
     */
    @PreAuthorize("@ss.hasPermi('stock:in:remove')")
    @Log(title = "入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(warehouseInService.deleteWarehouseInByIds(ids));
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('stock:out:edit')")
    @Log(title = "出库", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody WarehouseIn warehouseIn) {
        return toAjax(warehouseInService.updateWarehouseIn(warehouseIn));
    }

    /**
     * 获取采购入库编号
     * @return
     */
    @PreAuthorize("@ss.hasPermi('stock:out:getCGRK')")
    @Log(title = "获取采购入库编号")
    @GetMapping("getCGRK")
    public AjaxResult getCGRK(){
        return success(SeqNumberUtils.getCGRKNumber());
    }
}
